package es.xpt.ysf.commons.person.view;

import java.util.ArrayList;
import java.util.List;

import javax.faces.component.UIData;

import es.xpt.ysf.YSFException;
import es.xpt.ysf.commons.person.model.Person;
import es.xpt.ysf.commons.person.view.PersonDelegate;
import es.xpt.ysf.logging.LogManager;

@SuppressWarnings("serial")
public class PersonController extends PersonList {
	
	private String id;
	
	private Person searchPerson;
	
	private ArrayList<String[]> entities;
//	private UIData entityInfo;
	
	private String entityUrl;
	private String dependenceId;
	
	private List<String> dependences;
	private UIData dependenceInfo;
	
	public PersonController() {
		super();
		searchPerson = new Person();
	}
	
	@Override
	public Person getPerson() {
		if ( ( person == null || person.getId()==null) && id!=null && id.trim().length()>0) {
			try {
				person = new PersonDelegate(getUserBean()).getPerson(id);
				helper.setEditMode(true);
			} catch (YSFException e) {
				LogManager.getLog(getClass()).error(e.getMessage(), e);
			}
		}
		return person;
	}
	
	@Override
	public List<Person> getPeople() {
		return people;
	}

	public List<Person> searchPerson(){
		people = null;
		if (  searchPerson!=null) {
			try {
				people = new PersonDelegate(getUserBean()).searchPerson(searchPerson);
			} catch (YSFException e) {
				LogManager.getLog(getClass()).error(e.getMessage(), e);
			}
		}
		return people;
	}

	public String updatePerson() {
		try {
			  new PersonDelegate(getUserBean()).updatePerson(person);
			} catch (YSFException e) {
				super.setErrorMessage(e);
				LogManager.getLog(getClass()).error(e.getMessage(), e);
				return "failure";
			}
			super.setInfoMessage("commons.person.update");
			people = null;
			return "success";
	}
	
	public String insertPerson() {
		try {
			  new PersonDelegate(getUserBean()).insertPerson(person);
			} catch (YSFException e) {
				super.setErrorMessage(e);
				LogManager.getLog(getClass()).error(e.getMessage(), e);
				return "failure";
			}
			super.setInfoMessage("commons.person.update");
			people = null;
			return "success";
	}
	
	public String cancelEdit() {
		helper.setEditMode(false);
		people = searchPerson();
		return "success";
	}
	

	public String deletePerson() {
		person = (Person)helper.getHtmlDataTable().getRowData();
			try {
			  new PersonDelegate(getUserBean()).deletePerson(person.getId());
			} catch (YSFException e) {
				super.setErrorMessage(e);
				LogManager.getLog(getClass()).error(e.getMessage(), e);
				return "failure";
			}
		super.setInfoMessage("commons.person.remove");
		people = searchPerson();
		return "success";
	}

	public String deletePersonList() {
		 PersonDelegate p1 = new PersonDelegate(getUserBean());
		 if (!helper.getSelectedPersonList().isEmpty()){
			 List<String> listMessages;
			 try {
				 listMessages = p1.deleteBatchPeople(helper.getSelectedPersonList());
			 	 if (listMessages==null || listMessages.isEmpty()){
			 		super.setInfoMessage("commons.person.remove");
			 	 }else if( listMessages.size()>0) {
			 		for(int i=0;listMessages.size()>=(i+1);i=i+2){
			 			Person personTemp = new Person();
			 			personTemp.setId(listMessages.get(i+1));
			 			int j=people.indexOf(personTemp);
			 			super.setErrorMessage(listMessages.get(i),people.get(j).getName()+' '+people.get(j).getSurname());
			 		}
			 	 }
			 } catch (YSFException e) {
					super.setErrorMessage(e);							
					LogManager.getLog(getClass()).error(e.getMessage(), e);
					return "failure";
			}
		 }
		 people = searchPerson();		

		return "success";
	}
	
	public String editPerson() {
		helper.setEditMode(true);
		person = (Person)helper.getHtmlDataTable().getRowData();
		return "success";
	}
	
	public ArrayList<String[]> getEntities() {		
		if (entities == null && person!=null && person.getId()!=null){
			try {
				entities = new PersonDelegate(getUserBean()).searchDependentEntities(person.getId());
			} catch (YSFException e) {
				super.setErrorMessage(e);
				LogManager.getLog(getClass()).error(e.getMessage(), e);
			}
		}
		return entities;
	}

	
	public List<String> showDependences() {
		if (person != null){
			entity = ((String[]) helper.getData().getRowData())[1];
			if (entity != null) {
				try {
				  dependences = new PersonDelegate(getUserBean()).searchEntityDependences(person.getId(),entity);
				} catch (YSFException e) {
					super.setErrorMessage(e);
					LogManager.getLog(getClass()).error(e.getMessage(), e);
				}
			}
		}
		return dependences;
	}
	

	
// GETTERS and SETTERS
	

	public Person getSearchPerson() {
		return searchPerson;
	}

	public void setSearchPerson(Person searchPerson) {
		this.searchPerson = searchPerson;
	}

	public void setEntities(ArrayList<String[]> entities) {
		this.entities = entities;
	}

	public List<String> getDependences() {
		return dependences;
	}

	public void setDependences(List<String> dependences) {
		this.dependences = dependences;
	}

	public UIData getDependenceInfo() {
		return dependenceInfo;
	}

	public void setDependenceInfo(UIData dependenceInfo) {
		this.dependenceInfo = dependenceInfo;
	}


	public String getDependenceId() {
		return dependenceId;
	}

	public void setDependenceId(String dependenceId) {
		this.dependenceId = dependenceId;
	}
	
	public void setEntityUrl(String entityUrl) {
		this.entityUrl = entityUrl;
	}


	public String getId() {
		return id;
	}


	public void setId(String id) {
		this.id = id;
	}

	/**
	 * @return the entityUrl
	 */
	public String getEntityUrl() {
		return entityUrl;
	}
}



